package com.example.mapinformationandroid.Http;

import android.util.Log;

import com.example.mapinformationandroid.Model.Information;
import com.example.mapinformationandroid.Model.ResponseBase;
import com.example.mapinformationandroid.Model.User;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class HTTPBase {
    public static final int SUCCESS = 1;
    public static final int NOTFOND = -1;
    public static final int ERROR = 0;
    public static final int Online = 1;
    public static final int Offline = -1;
    public static final int examine = 0;
    public static final int token_invalid = 1000;
    private APIList apiList;

    public HTTPBase() {
//        Retrofit mRetrofit = new Retrofit.Builder().baseUrl("https://121.36.9.1:8000/")
//                //增加返回值为Gson的支持(以实体类返回)
//                .addConverterFactory(GsonConverterFactory.create())
//                //增加返回值为String的支持
//                .addConverterFactory(ScalarsConverterFactory.create())
//                //增加返回值为Oservable<T>的支持
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                //自定义OkHttpClient
//                .client(client)
//                .build();
//
//        APIList apiList = mRetrofit.create(APIList.class);
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();
        Retrofit mRetrofit = new Retrofit.Builder().baseUrl("http://121.36.9.1:8000/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(client)
                .build();
        apiList = mRetrofit.create(APIList.class);
    }

    public void getInformationListByPost(int postcodes,String token,Observer<ResponseBase<List<Information>>> observer) {
        apiList.getInformation_byPostcodes(postcodes,token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    public void getInformationListByUser(int user_id,String token,Observer<ResponseBase<List<Information>>> observer) {
        apiList.getInformation_byuser(user_id,token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }


    public void login(Map<String, String> map, Observer<ResponseBase<User>> observer) {
        apiList.Login(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    public void sign_in(Map<String, String> map, Observer<ResponseBase> observer) {
        apiList.Register(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public void create_information(Map<String, String> information, Observer<ResponseBase> observer) {
        apiList.Create_Information(information)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}


